﻿using Azure;
using MediatR;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Newtonsoft.Json;
using System.ComponentModel.DataAnnotations;
using W.Common;
using W.Warehouse.Domain;
using W.Warehouse.Domain.DTO.ReportlossesDto;
using W.Warehouse.Read.Api.Application.Command.ReportlossesCommand;
using W.Warehouse.Repository.Interface;
using static WMSpublic.ParseAPI;

namespace W.Warehouse.Read.Api.Application.CommandHandlder.ReportlossesCommandHandler
{
    public class ReportlossesSelectCommandHandler : IRequestHandler<ReportlossesSelectCommand, PageReportlosses>
    {

        private readonly IReportlossesRepository reportlossesRepository;
        private readonly IReportProductRepository reportproductRepository;
        private readonly IWarehouseInfoRepository warehouseInfoRepository;

        public ReportlossesSelectCommandHandler (IReportlossesRepository reportlossesRepository,
            IReportProductRepository reportproductRepository, IWarehouseInfoRepository warehouseInfoRepository)
        {
            this.reportlossesRepository = reportlossesRepository;
            this.reportproductRepository = reportproductRepository;
            this.warehouseInfoRepository = warehouseInfoRepository;

        }
        /// <summary>
        /// 显示报损
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<PageReportlosses> Handle(ReportlossesSelectCommand request, CancellationToken cancellationToken)
        {

            //List<WarehouseInfo> warehouses = await GetWarehouseShow(1, 100);
            //List<Products> product = await GetProductsList(1, 100);
            var list = (from a in await reportlossesRepository.GetAllAsync()
                        join b in await reportproductRepository.GetAllAsync()
                        on a.ReportlossesId equals b.ReportlossesId
                        where b.IsDelete==false && a.IsDelete == false
                        select new ReportlossesInfoDto
                        {
                            ReportlossesId = a.ReportlossesId,
                            ProductId = a.ProductId,
                            ReportlossesNo = a.ReportlossesNo,
                            ProductName = b.ProductName,
                            ProductNo = b.ProductNo,
                            Pici = b.ProductNo,
                            ProductSpec = b.ProductSpec,
                            ReportNum = b.ReportNum,
                            WarehouseName=b.BinLocationName,
                            Wlocator = a.Wlocator,
                            Batch=b.Batch,
                            WRepolocator = a.WRepolocator,
                            ReportlossesType = a.ReportlossesType,
                            DocumentState = a.DocumentState,
                            ExaminePeople = a.ExaminePeople,
                            ExamineTime = a.ExamineTime,
                        }).ToList();
            if (!string.IsNullOrEmpty(request.ReportlossesNo))
            {
                list = list.Where(x => x.ReportlossesNo == request.ReportlossesNo).ToList();
            }
            if (!string.IsNullOrEmpty(request.ReportlossesType))
            {
                list = list.Where(x => x.ReportlossesType == request.ReportlossesType).ToList();
            }
            if (request.DocumentState == Common.Enum.AuditStatus.全部)
            {
                list = list.Where(x => x.DocumentState == request.DocumentState).ToList();
            }
            PageReportlosses page=new PageReportlosses();
            page.tacount = list.Count();
            page.pacount = (int)Math.Ceiling(page.tacount * 1.0 / request.size);
            page.List = list.OrderByDescending(x => x.ReportlossesId).Skip((request.index - 1) * request.size).Take(request.size).ToList();
            return page;
        }

        ///// <summary>
        ///// 仓库类型显示
        ///// </summary>
        ///// <returns></returns>
        //public async Task<dynamic> GetWarehouseShow(int pageIndex, int pageSize)
        //{
        //    var token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiI0Iiwic2lkIjoiMTg1OTgwNTA4MDc2NTczMDgxNiIsImh0dHA6Ly9zY2hlbWFzLm1pY3Jvc29mdC5jb20vd3MvMjAwOC8wNi9pZGVudGl0eS9jbGFpbXMvZ3JvdXBzaWQiOiIiLCJqdGkiOiI5MWU1MzVhMy1kZjVlLTQxNGQtODdkMy0zYjE3MmIzNmRhZWEiLCJodHRwOi8vc2NoZW1hcy54bWxzb2FwLm9yZy93cy8yMDA1LzA1L2lkZW50aXR5L2NsYWltcy9uYW1lIjoi5LqO5a2Q5ra1IiwiaHR0cDovL3NjaGVtYXMueG1sc29hcC5vcmcvd3MvMjAwNS8wNS9pZGVudGl0eS9jbGFpbXMvdXJpIjoiLyIsImlzcyI6IlJlbmFvYm8yMDA0IiwiYXVkIjoiV2VpZnV3dTIyMDNBIn0.9e371kkhbGOGIALMM210Z0Mhka1BhADJFTKFd8w3AJY";
        //    HttpClientWrapper client = new HttpClientWrapper(token);
        //    string url = "http://10.223.8.18:8000/warehouseRead/api/WarehouseMange/GetWarehouse?pageIndex=" + pageIndex + "&pageSize=" + pageSize; ;
        //    var response = await client.GetAsync(url);
        //    var json = JsonConvert.DeserializeObject<ApiResultList<WarehouseInfo>>(response);
        //    return json.data.list;
        //}

        //public async Task<dynamic> GEtProductShow(int index, int size)
        //{
        //    var token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiI0Iiwic2lkIjoiMTg1OTgwNTA4MDc2NTczMDgxNiIsImh0dHA6Ly9zY2hlbWFzLm1pY3Jvc29mdC5jb20vd3MvMjAwOC8wNi9pZGVudGl0eS9jbGFpbXMvZ3JvdXBzaWQiOiIiLCJqdGkiOiI5MWU1MzVhMy1kZjVlLTQxNGQtODdkMy0zYjE3MmIzNmRhZWEiLCJodHRwOi8vc2NoZW1hcy54bWxzb2FwLm9yZy93cy8yMDA1LzA1L2lkZW50aXR5L2NsYWltcy9uYW1lIjoi5LqO5a2Q5ra1IiwiaHR0cDovL3NjaGVtYXMueG1sc29hcC5vcmcvd3MvMjAwNS8wNS9pZGVudGl0eS9jbGFpbXMvdXJpIjoiLyIsImlzcyI6IlJlbmFvYm8yMDA0IiwiYXVkIjoiV2VpZnV3dTIyMDNBIn0.9e371kkhbGOGIALMM210Z0Mhka1BhADJFTKFd8w3AJY";
        //    HttpClientWrapper client = new HttpClientWrapper(token);
        //    string url = "http://10.223.8.18:8000/productRead/api/ProductSelect/GetProductInfos?index=" + index + "&size=" + size;
        //    var respomse = await client.GetAsync(url);
        //    var json = JsonConvert.DeserializeObject<ApiResultList<ProductInfo>>(respomse);
        //    return json.data.list;
        //}

        ///// <summary>
        ///// 仓库管理
        ///// </summary>
        //public class WarehouseInfo
        //{
        //    public int WarehouseId { get; set; }
        //    /// <summary>
        //    /// 仓库名称
        //    /// </summary>
        //    public string? WarehouseName { get; set; }
        //}


        //public class ProductInfo
        //{
        //    /// <summary>
        //    /// 产品id
        //    /// </summary>
        //    public int ProductId { get; set; }
        //    /// <summary>
        //    /// 产品编号
        //    /// </summary>
        //    public string? ProductNo { get; set; }
        //    /// <summary>
        //    /// 产品名称
        //    /// </summary>
        //    public string? ProductName { get; set; }
        //    /// <summary>
        //    /// 厂家编码
        //    /// </summary>
        //    public string? ManuNo { get; set; }
        //    /// <summary>
        //    /// 内部编码
        //    /// </summary>
        //    public string? InsideNo { get; set; }
        //    /// <summary>
        //    /// 单位
        //    /// </summary>
        //    public int? UnitId { get; set; }
        //    /// <summary>
        //    /// 类别
        //    /// </summary>
        //    public int? ProductTypeId { get; set; }
        //    /// <summary>
        //    /// 上线值
        //    /// </summary>
        //    public int? GoOnline { get; set; }
        //    /// <summary>
        //    /// 下限值
        //    /// </summary>
        //    public int? Online { get; set; }
        //    /// <summary>
        //    /// 规格
        //    /// </summary>
        //    public string? ProductSpec { get; set; }
        //    /// <summary>
        //    /// 价格
        //    /// </summary>
        //    public decimal? ProductPrice { get; set; }
        //    /// <summary>
        //    /// 默认仓库
        //    /// </summary>
        //    public int? WarehouseId { get; set; }
        //    /// <summary>
        //    /// 默认库位
        //    /// </summary>
        //    public int? BinLocationId { get; set; }
        //    /// <summary>
        //    /// 默认供应商
        //    /// </summary>
        //    public string? VendorId { get; set; }
        //    /// <summary>
        //    /// 默认客户
        //    /// </summary>
        //    public string? ClientId { get; set; }
        //    /// <summary>
        //    /// 描述
        //    /// </summary>
        //    public string? ProductDesc { get; set; }
        //    /// <summary>
        //    /// 包装类型
        //    /// </summary>
        //    public string? ProductWrap { get; set; }
        //    /// <summary>
        //    /// 重量
        //    /// </summary>
        //    public string? ProductWeight { get; set; }
        //    /// <summary>
        //    /// 公司Id
        //    /// </summary>
        //    public string? officeId { get; set; }
        //    /// <summary>
        //    /// 用户Id
        //    /// </summary>
        //    public int? userid { get; set; }
        //}
    }
}
